package cake.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import cake.dao.AddressDao;
import cake.entity.Address;

public class AddressDaoImpl implements AddressDao {
	private JdbcTemplate jt = new JdbcTemplate();

	private static class AddressDaoRowMapper implements RowMapper<Address> {
		public Address mapRow(ResultSet rs) throws SQLException {
			return new Address(rs.getInt("id"), rs.getInt("uid"), rs.getString("address"), rs.getString("phone"),
					rs.getString("consignee"));
		}

	}

	public boolean addAddress(Address address) {
		String sql="insert into address(uid,address,phone,consignee) values(?,?,?,?)";
		return jt.update(sql,address.getUid(),address.getAddress(),address.getPhone(),address.getConsignee());
	}

	@Override
	public boolean updateAddress(Address address,int id) {
		String sql="update address set uid=?,address=?,phone=?,consignee=? where id=?";
		return jt.update(sql, address.getUid(),address.getAddress(),address.getPhone(),address.getConsignee(),id);
	}

	@Override
	public boolean deleteAddressByid(int id) {
		String sql="delete from address where id=?";
		return jt.update(sql, id);
	}

	@Override
	public List<Address> findAddressByUid(int uid) {
		String sql="select * from address where uid=?";
		return jt.query(sql, new AddressDaoRowMapper(),uid);
	}

	@Override
	public List<Address> getAllAddress() {
		return jt.query("select * from address",new AddressDaoRowMapper());
	}
	
	public List<Address> getAddressByUid(int uid){
		String sql="select * from address where uid=?";
		return jt.query(sql, new AddressDaoRowMapper(),uid);
	}
	@Override
	public Address getAddressById(int aid) {
		String sql="select * from address where id=?";
		return jt.queryOne(sql, new AddressDaoRowMapper(),aid);
	}
	public static void main(String[] args) {
//		AddressDaoImpl a = new AddressDaoImpl();
//		List<Address> list = a.getAddressByUid(1);
//		for(Address aa:list) {
//			System.out.println(aa.getAddress());
//		}
	}
}
